package com.stars.common;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author csot.qhluodajie on 2019/1/2.
 */
public class MyDateUtils {
    public static final String YYYY_MM_DD_HH_MM_SS_ = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYY_MM_DD_ = "yyyy-MM-dd";
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyyMMddHHmmss";
    public static final String YYYY_MM_DD_HH_MM_SS_SSS = "yyyyMMddHHmmssSSS";
    public static final String YYYY_MM_DD = "yyyyMMdd";
    public static final String YYYY_MM = "yyyyMM";
    public static final String YYYY_MM_CN = "yyyy年MM月";
    public static final String YYYY_MM_DD_CN = "yyyy年MM月dd日";
    public static final String YYYY = "yyyy";
    public static final String MM = "MM";
    public static final String DAY = "day";
    public static final String MONTH = "month";
    public static final String YEAR = "year";

    public MyDateUtils() {
    }

    public static Date getCurrentDate() {
        Calendar currentDate = Calendar.getInstance();
        currentDate.set(11, 0);
        currentDate.set(12, 0);
        currentDate.set(13, 0);
        return currentDate.getTime();
    }

    public static String timestampe() {
        return format(new Date(), YYYY_MM_DD_HH_MM_SS_SSS);
    }

    public static String format(Date date, String formatter) {
        SimpleDateFormat formatTime = new SimpleDateFormat(formatter);
        return date != null ? formatTime.format(date) : "";
    }

    public static Date format(String date, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        if (date != null) {
            ParsePosition pos = new ParsePosition(0);
            return formatter.parse(date, pos);
        } else {
            return null;
        }
    }
    public static String formatStr(String date, String format) {
        Date date1 = format(date, format);
        return format(date1,format);
    }
    /**
     * 当前时间
     *
     * @param formater
     * @return
     */
    public static Date getNowDate(String formater) {
        String format = format(new Date(), YYYY_MM_DD_HH_MM_SS);
        return format(format, formater);
    }

    /**
     * 时间比大小
     *
     * @param time1
     * @param time2
     * @return
     * @throws ParseException
     */
    public static boolean compare(String time1, String time2) throws ParseException {
        if (time1.equals(time2)) {
            return true;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //将字符串形式的时间转化为Date类型的时间
        Date a = sdf.parse(time1);
        Date b = sdf.parse(time2);
        //Date类的一个方法，如果a早于b返回true，否则返回false
        if (a.before(b))
            return true;
        else
            return false;
    }

    /**
     * 两个时间差天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDaysByMillisecond(Date date1, Date date2) {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
        return days;
    }

    /**
     * 获取本月第一天：
     *
     * @return string yyyy-MM-dd
     */
    public static String getMonthFirstDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, 0);
        Date date = calendar.getTime();
        return MyDateUtils.format(date, MyDateUtils.YYYY_MM_DD_);
    }

    /**
     * 获取本月最后一天：
     *
     * @return string yyyy-MM-dd
     */
    public static String getMonthLastDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        calendar.add(Calendar.MONTH, 1);
        Date date = calendar.getTime();
        return MyDateUtils.format(date, YYYY_MM_DD_);
    }
    /**
     * 获取指定年月的第一天
     * @param year
     * @param month
     * @return
     */
    public static String getFirstDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最小天数
        int firstDay = cal.getMinimum(Calendar.DATE);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH,firstDay);
        return format(cal.getTime(), YYYY_MM_DD_);
    }
    /**
     * 获取指定年月的最后一天
     * @param year
     * @param month
     * @return
     */
    public static String getLastDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DATE);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        return format(cal.getTime(), YYYY_MM_DD_);
    }
    /**
     * 获取某年第一天日期
     * @param year 年份
     * @return Date
     */
    public static String getYearFirst(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        return format(calendar.getTime(), YYYY_MM_DD_);
    }

    /**
     * 获取某年最后一天日期
     * @param year 年份
     * @return Date
     */
    public static String getYearLast(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        return format(calendar.getTime(), YYYY_MM_DD_);
    }

    /**
     * 当前年份
     * @return
     */
    public static String getCurrentYear() {
        return format(getCurrentDate(), YYYY);
    }
    //时间年份
    public static Integer getCurrentYear(String date){
        if(StringUtils.isBlank(date)){
            return 0;
        }
        Date date1 = format(date, YYYY_MM_DD_);
        String year = format(date1, YYYY);
        return Integer.parseInt(year);
    }
    //时间月份
    public static Integer getCurrentMonth(String date){
        if(StringUtils.isBlank(date)){
            return 0;
        }
        Date date1 = format(date, YYYY_MM_DD_);
        String month = format(date1, MM);
        return Integer.parseInt(month);
    }


    public static void main(String[] args) throws Exception {
        boolean result = compare("2012-11-11", "2012-11-30");
        System.out.println(result);
        System.out.println(getMonthFirstDay());
        System.out.println(getYearLast(2019));
       String month = format(new Date(), YYYY);
        System.out.println(month);
    }

}
